//
//  SplashViewController.m
//  nonghubao
//
//  Created by Fenrir on 13-4-18.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//



#import "SplashViewController.h"
#import "Config.h"
#import "MobileFormViewController.h"
#import "LoginFormViewController.h"
#import "Helper.h"
#import "Option.h"
#import "DBKit.h"

@interface SplashViewController ()

@end

@implementation SplashViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, _SYS_SCREEN_HEIGHT)];
    _scrollView.contentSize = CGSizeMake(SPLASH_PICTURE_NUM * 320.0f, _SYS_SCREEN_HEIGHT);
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.pagingEnabled = YES;
    
    self.view = _scrollView;
    
    for (int i = 0; i < SPLASH_PICTURE_NUM; i++) {
        
        //添加图片
        UIView *pictureView = [[UIView alloc] initWithFrame:CGRectMake(i * 320.0f, 0.0f, 320.0f, _SYS_SCREEN_HEIGHT)];
        
        UIImage *image = nil;
        NSString *name = nil;
        
        if (iPhone5) {
            name = [NSString stringWithFormat:@"com_img_00%d_b-568h@2x", i+1];
            
            image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"jpg"]];
        }else{
            name = [NSString stringWithFormat:@"com_img_00%d_n@2x", i+1];
            
            image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:name ofType:@"jpg"]];
        }
        
        pictureView.backgroundColor = [UIColor colorWithPatternImage:image];
        
        //最后一张图片时，加入注册登录按钮
        if (i == SPLASH_PICTURE_NUM-1) {
            
            UIButton* registerButton = [[UIButton alloc] initWithFrame:CGRectMake(10.0f, _SYS_SCREEN_HEIGHT - 70.0f, 145.0f, 40.0f)];
            if (iPhone5) {
                registerButton.frame = CGRectMake(10.0f, _SYS_SCREEN_HEIGHT - 80.0f, 145.0f, 40.0f);
            }
            
            registerButton.titleLabel.font = _SYS_FONT_006;
            [registerButton setBackgroundImage:[UIImage imageNamed:@"com_btn_024_n"] forState:UIControlStateNormal];
            [registerButton setTitle:@"注册" forState:UIControlStateNormal];
            [registerButton setTitleColor:[UIColor colorWithRed:124/255.0f green:167/255.0f blue:2/255.0f alpha:1.0f] forState:UIControlStateNormal];
            [registerButton addTarget:self action:@selector(registerButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
            
            [pictureView addSubview:registerButton];
            
            UIButton* loginButton = [[UIButton alloc] initWithFrame:CGRectMake(registerButton.frame.origin.x + registerButton.frame.size.width + 10.0f, _SYS_SCREEN_HEIGHT - 70.0f, 145.0f, 40.0f)];
            
            if (iPhone5) {
                loginButton.frame = CGRectMake(registerButton.frame.origin.x + registerButton.frame.size.width + 10.0f, _SYS_SCREEN_HEIGHT - 80.0f, 145.0f, 40.0f);
            }
            loginButton.titleLabel.font = _SYS_FONT_006;
            [loginButton setBackgroundImage:[UIImage imageNamed:@"com_btn_025_n"] forState:UIControlStateNormal];
            [loginButton setTitle:@"登录" forState:UIControlStateNormal];
            [loginButton setTitleColor:[UIColor colorWithRed:255/255.0f green:255/255.0f blue:253/255.0f alpha:1.0f] forState:UIControlStateNormal];
            [loginButton addTarget:self action:@selector(loginButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
         
            [pictureView addSubview:loginButton];
            
            
            if (SPLASH_BUTTONS_POSITION == 1) {
                registerButton.frame = CGRectMake(registerButton.frame.origin.x, registerButton.frame.origin.y-20.0f, registerButton.frame.size.width, registerButton.frame.size.height);
                loginButton.frame = CGRectMake(loginButton.frame.origin.x, loginButton.frame.origin.y-20.0f, loginButton.frame.size.width, loginButton.frame.size.height);
            }
        }
        
        [_scrollView addSubview:pictureView];
    }
    
    NSArray *windows = [UIApplication sharedApplication].windows;
    UIWindow *window = [windows objectAtIndex:0];
    NSLog(@"\n\n-- DEBUG.0000000 --\n\nwindow.subviews.count=%i\n%@\n%@", window.subviews.count, window.subviews, window.rootViewController.view);
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
}

#pragma mark - 自定义事件

- (void)registerButtonEvent:(id)sender
{
    NSLog(@"注册");
    //设定一个全局的值，用于MobileFormVC和PasswordFormVC判断是注册还是找回密码
    [[NSUserDefaults standardUserDefaults] setBool:true forKey:@"isRegisterOrNot"];
    
    MobileFormViewController *mobileFormVC = [[MobileFormViewController alloc] init];
    mobileFormVC.title = @"注册";
    [mobileFormVC agreementAppear];
    //使用一个NavigationController来包裹注册视图
    UINavigationController* mobileFomNC = [[UINavigationController alloc] initWithRootViewController:mobileFormVC];
    [Helper navigationControllerCommonStyle:mobileFomNC];
    
    //使用presentModal方式
    [self presentModalViewController:mobileFomNC animated:true];
    
    [self saveSplashEndStatus];
}

- (void)loginButtonEvent:(id)sender
{
    NSLog(@"登陆");
    //设定一个全局的值，用于MobileFormVC判断是注册还是找回密码
    [[NSUserDefaults standardUserDefaults] setBool:false forKey:@"isRegisterOrNot"];
    
    //创建登录视图
    LoginFormViewController* loginFormVC = [[LoginFormViewController alloc] init];
    loginFormVC.title = @"登录";
    
    //使用一个NavigationController来包裹登录视图
    UINavigationController* loginFormNC = [[UINavigationController alloc] initWithRootViewController:loginFormVC];
    [Helper navigationControllerCommonStyle:loginFormNC];
    
    //使用presentModal方式
    [self presentModalViewController:loginFormNC animated:true];
    
    [self saveSplashEndStatus];
}

#pragma mark - 私有方法

//将splash的状态设为1，第一次使用APP后，不再在app启动时，显示splash
- (void)saveSplashEndStatus
{
    Option *option = [[Option alloc] init];
    
    option.name = @"sysSplashShown";
    option.value = @"1";
    [[DBKit sharedDBKit] save:option];
}

@end
